package com.workManager.util;



import com.workManager.Constants;
import com.workManager.vo.CurrentUserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

/**
 * Web层辅助类
 */
public final class WebUtil {
	private WebUtil() {
	}
	
	static Pattern pattern = Pattern.compile("[0-9]*");
	/**
	 * 
	* @Title: getEntId 
	* @Description: 获取企业号ID
	* @param @param request
	* @param @return 
	* @return String
	* @author xucan  
	* @throws
	 */
/*	public static final String getEntId(HttpServletRequest request, boolean fromSession){
		String entId = null;
		if(fromSession){
			CurrentUserVO vo = getCurrentUserVO();
			if(vo != null){
				entId = vo
			}
		}
		if(StringUtils.isBlank(entId)){
			String reg = ".*\\/\\/([^\\/\\.]*).*";
			String url = request.getRequestURL().toString();
			System.out.println("getEntId->url:"+url);
			String str = url.replaceAll (reg, "$1");
			if(pattern.matcher(str).matches()){
				entId = str;
			}
		}
		if(StringUtils.isBlank(entId)){
			entId = request.getParameter("entId");
		}
		return entId;
	}*/

	/**
	 * 获取指定Cookie的值
	 * 
	 * @param //cookies cookie集合
	 * @param //cookieName cookie名字
	 * @param //defaultValue 缺省值
	 * @return
	 */
	public static final String getCookieValue(HttpServletRequest request, String cookieName, String defaultValue) {
		Cookie cookie = WebUtils.getCookie(request, cookieName);
		if (cookie == null) {
			return defaultValue;
		}
		return cookie.getValue();
	}

	/** 保存当前用户 */
	public static final void saveCurrentUser(Object user) {
		setSession(Constants.CURRENT_USER, user);
	}

	/** 获取当前用户 */
	public static final String getCurrentUser() {
		Subject currentUser = SecurityUtils.getSubject();
		if (null != currentUser) {
			Session session = currentUser.getSession();
			if (null != session) {
				return (String) session.getAttribute(Constants.CURRENT_USER);
			}
		}
		return null;
	}
	
	/** 获取当前用户对象 */
	public static final CurrentUserVO getCurrentUserVO() {
		Subject currentUser = SecurityUtils.getSubject();
		if (null != currentUser) {
			Session session = currentUser.getSession();
			if (null != session) {
				return (CurrentUserVO) session.getAttribute(Constants.CURRENT_USER_VO);
			}
		}
		return null;
	}
	
	/** 保存当前用户对象 */
	public static final void saveCurrentUser(CurrentUserVO user) {
		setSession(Constants.CURRENT_USER_VO, user);
	}

	/**
	 * 将一些数据放到ShiroSession中,以便于其它地方使用
	 * 
	 * @see //比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到
	 */
	public static final void setSession(Object key, Object value) {
		Subject currentUser = SecurityUtils.getSubject();
		if (null != currentUser) {
			Session session = currentUser.getSession();
			if (null != session) {
				session.setAttribute(key, value);
			}
		}
	}

	/** 移除当前用户 */
	public static final void removeCurrentUser(HttpServletRequest request) {
		request.getSession().removeAttribute(Constants.CURRENT_USER);
	}

	/**
	 * 获得国际化信息
	 * 
	 * @param key 键
	 * @param request
	 * @return
	 */
	public static final String getApplicationResource(String key, HttpServletRequest request) {
		ResourceBundle resourceBundle = ResourceBundle.getBundle("ApplicationResources", request.getLocale());
		return resourceBundle.getString(key);
	}

	/**
	 * 获得参数Map
	 * 
	 * @param request
	 * @return
	 */
	public static final Map<String, Object> getParameterMap(HttpServletRequest request) {
		return WebUtils.getParametersStartingWith(request, null);
	}

	/** 获取客户端IP */
	public static final String getHost(HttpServletRequest request) {
		String ip = request.getHeader("X-Forwarded-For");
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}
		if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if ("127.0.0.1".equals(ip)) {
			InetAddress inet = null;
			try { // 根据网卡取本机配置的IP
				inet = InetAddress.getLocalHost();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			if(inet == null){
				return "";
			}
			ip = inet.getHostAddress();
		}
		// 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
		if (ip != null && ip.length() > 15) {
			if (ip.indexOf(",") > 0) {
				ip = ip.substring(0, ip.indexOf(","));
			}
		}
		return ip;
	}

	public static final Map<String,Object> buildResultMap(String status, String code, String msg, Object data){
		Map<String,Object> map = new HashMap();
		map.put("status",status);
		map.put("code",code);
		map.put("message",msg);
		map.put("data",data);
		return map;
	}

}
